﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.Button
{
    public static class ButtonExtensions
    {
        public static ButtonBuilder Button(this ViewComponentFactory viewComponentFactory)
        {
            return Button(viewComponentFactory, null);
        }

        public static ButtonBuilder Button(this ViewComponentFactory viewComponentFactory, object htmlAttributes)
        {
            Button button = new Button(viewComponentFactory.Writer, ButtonType.Default);

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder SplitButton(this ViewComponentFactory viewComponentFactory)
        {
            return SplitButton(viewComponentFactory, null);
        }

        public static ButtonBuilder SplitButton(this ViewComponentFactory viewComponentFactory, object htmlAttributes)
        {
            Button button = new Button(viewComponentFactory.Writer, ButtonType.SplitButton);

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder LinkButton(this ViewComponentFactory viewComponentFactory, string url)
        {
            return LinkButton(viewComponentFactory, url, null);
        }

        public static ButtonBuilder LinkButton(this ViewComponentFactory viewComponentFactory, string url, object htmlAttributes)
        {
            return LinkButton(viewComponentFactory, url, null, htmlAttributes);
        }

        public static ButtonBuilder LinkButton(this ViewComponentFactory viewComponentFactory, string url, string target, object htmlAttributes)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            Button button = new Button(viewComponentFactory.Writer, ButtonType.LinkButton)
            {
                LinkToUrl = url,
                LinkToTarget = target
            };

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder SplitLinkButton(this ViewComponentFactory viewComponentFactory, string url)
        {
            return SplitLinkButton(viewComponentFactory, url, null);
        }

        public static ButtonBuilder SplitLinkButton(this ViewComponentFactory viewComponentFactory, string url, object htmlAttributes)
        {
            return SplitLinkButton(viewComponentFactory, url, null, htmlAttributes);
        }

        public static ButtonBuilder SplitLinkButton(this ViewComponentFactory viewComponentFactory, string url, string target, object htmlAttributes)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            Button button = new Button(viewComponentFactory.Writer, ButtonType.SplitLinkButton)
            {
                LinkToUrl = url,
                LinkToTarget = target
            };

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder SubmitButton(this ViewComponentFactory viewComponentFactory)
        {
            return SubmitButton(viewComponentFactory, null, null, null);
        }

        public static ButtonBuilder SubmitButton(this ViewComponentFactory viewComponentFactory, object htmlAttributes)
        {
            return SubmitButton(viewComponentFactory, null, null, htmlAttributes);
        }

        public static ButtonBuilder SubmitButton(this ViewComponentFactory viewComponentFactory, string name, string value, object htmlAttributes)
        {
            Button button = new Button(viewComponentFactory.Writer, ButtonType.InputButton)
            {
                InputType = "submit"
            };

            if (!string.IsNullOrEmpty(name))
            {
                button.InputName = name;
            }

            if (!string.IsNullOrEmpty(value))
            {
                button.InputValue = value;
            }

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder SplitSubmitButton(this ViewComponentFactory viewComponentFactory)
        {
            return SplitSubmitButton(viewComponentFactory, null, null, null);
        }

        public static ButtonBuilder SplitSubmitButton(this ViewComponentFactory viewComponentFactory, object htmlAttributes)
        {
            return SplitSubmitButton(viewComponentFactory, null, null, htmlAttributes);
        }

        public static ButtonBuilder SplitSubmitButton(this ViewComponentFactory viewComponentFactory, string name, string value, object htmlAttributes)
        {
            Button button = new Button(viewComponentFactory.Writer, ButtonType.SplitInputButton)
            {
                InputType = "submit"
            };

            if (!string.IsNullOrEmpty(name))
            {
                button.InputName = name;
            }

            if (!string.IsNullOrEmpty(value))
            {
                button.InputValue = value;
            }

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }

        public static ButtonBuilder DropDownButton(this ViewComponentFactory viewComponentFactory)
        {
            return DropDownButton(viewComponentFactory, null);
        }

        public static ButtonBuilder DropDownButton(this ViewComponentFactory viewComponentFactory, object htmlAttributes)
        {
            Button button = new Button(viewComponentFactory.Writer, ButtonType.DropDownButton);

            if (htmlAttributes != null)
            {
                button.HtmlAttributes.Merge(htmlAttributes, true);
            }

            return new ButtonBuilder(button);
        }
    }
}
